package com.kbbywyclient.util;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;


/**
 * Description: View Util
 * Created by FHW
 * on 2017/3/27
 */
public class ViewUtils {
    public static void getFocus(EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
    }

    /**
     * 获取TextView的内容
     *
     * @param textView
     * @return
     */
    public static String getText(TextView textView) {
        String text = "";
        if (textView != null) {
            text = textView.getText().toString().trim();
        }
        return text;
    }

    /**
     * 设置TextView内容
     *
     * @param textView
     * @param content
     */
    public static void setText(TextView textView, String content) {
        setText(textView, content, "");
    }

    public static void setText(TextView textView, String content, TextView.BufferType type) {
        setText(textView, content, "", type);
    }

    /**
     * 设置TextView内容，带格式
     *
     * @param textView
     * @param content
     * @param values
     */
    public static void setTextFormat(TextView textView, String content, Object... values) {
        setText(textView, formatString(content, values), "");
    }


    public static void setHtmlText(TextView textView, String content) {
        if (textView == null) {
            return;
        }
        if (!StringUtil.isEmpty(content)) {
            textView.setText(Html.fromHtml(content));
        } else {
            textView.setText("");
        }
    }


    /**
     * 设置TextView内容
     *
     * @param textView
     * @param content
     * @param defaultContent
     */
    public static void setText(TextView textView, String content, String defaultContent) {
        if (textView == null) {
            return;
        }
        if (!StringUtil.isEmpty(content)) {
            textView.setText(content);
        } else {
            textView.setText(defaultContent);
        }
    }

    public static void setText(TextView textView, String content, String defaultContent, TextView.BufferType type) {
        if (textView == null) {
            return;
        }
        if (!StringUtil.isEmpty(content)) {
            textView.setText(content, type);
        } else {
            textView.setText(defaultContent, type);
        }
    }

    /**
     * 设置TextView内容
     *
     * @param textView
     * @param res
     */
    public static void setText(TextView textView, int res) {
        if (textView == null) {
            return;
        }
        try {
            textView.setText(res);
        } catch (Exception e) {
            e.printStackTrace();
            textView.setText("");
        }
    }

    public static void setTextColor(Context context, TextView textView, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            textView.setTextColor(context.getResources().getColor(color, null));
        } else {
            textView.setTextColor(context.getResources().getColor(color));
        }
    }

    public static void setTextDrawable(Context context, TextView textView, int left, int top, int right, int bottom) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            textView.setCompoundDrawables(getDrawableByRes(context, left), getDrawableByRes(context, top), getDrawableByRes(context, right), getDrawableByRes(context, bottom));
        } else {
            textView.setCompoundDrawablesRelativeWithIntrinsicBounds(left, top, right, bottom);
        }
    }

    public static Drawable getDrawableByRes(Context context, int res) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            return context.getResources().getDrawable(res);
        } else {
            return context.getResources().getDrawable(res, null);
        }
    }


    /**
     * 格式化字符串
     *
     * @param content
     * @param values
     * @return
     */
    public static String formatString(String content, Object... values) {
        return String.format(content, values);
    }

    public static void clearText(TextView textView) {
        if (textView != null) {
            textView.setText("");
        }
    }

    public static boolean keyboardShown(View rootView) {
        final int softKeyboardHeight = 100;
        Rect r = new Rect();
        rootView.getWindowVisibleDisplayFrame(r);
        DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
        int heightDiff = rootView.getBottom() - r.bottom;
        return heightDiff > softKeyboardHeight * dm.density;
    }

    public static void setViewAnim(int direction, View view) {
        ObjectAnimator animtor = null;
        //开始新的动画之前要先取消以前的动画
        if (animtor != null && animtor.isRunning()) {
            animtor.cancel();
        }
        //toolbar.getTranslationY()获取的是Toolbar距离自己顶部的距离
        float translationY = view.getTranslationY();
        if (direction == 0) {//下
            animtor = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, -view.getHeight(), 0.0f);
        } else if (direction == 1) {//上
            animtor = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, 0.0f, -view.getHeight());
        }

        animtor.start();

    }

    /**
     * 调用拨号盘拨打电话
     *
     * @param context
     * @param phoneNumber
     */
    public static void dialPhoneNumber(Context context, String phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + phoneNumber));
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }

    /**
     * 以屏幕宽度为基准，按指定比列配置view的高度 xbanner，750，150
     *
     * @param view        需要配置的view
     * @param widthRatio  宽度占比
     * @param heightRatio 高度占比
     */
    public static void configViewHeight(@NonNull View view, float widthRatio, float heightRatio) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (params != null) {
            DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
            final int width = metrics.widthPixels;
            params.width = width;
            params.height = (int) (width * heightRatio / widthRatio);
            view.setLayoutParams(params);
        }
    }

}
